/*
 * Copyright (C) 2017 Zhejiang lishiots Technology CO.,LTD.
 * All rights reserved.
 * Official Web Site: http://www.lishiots.com.
 * Developer Web Site: http://open.lishiots.com.
 */

package com.myjava.utils;

import com.myjava.common.ExcelVo;
import org.apache.poi.hssf.usermodel.*;
import org.apache.poi.ss.usermodel.Font;

import java.util.*;

/***
 * excel导出工具
 * @since:lishi-dc 1.0
 * @author <a href="mailto:xiaoymin@foxmail.com">xiaoymin@foxmail.com</a> 
 * 2017/12/21 16:59
 */
public class ExcelUtils {


    /***
     * 导出excel
     * 简单样式,单行,不包括单元格合并
     * @param excelVoList
     * @param data
     */
    public static HSSFWorkbook simpleExport(List<ExcelVo> excelVoList, List<Map<String, Object>> data) {
        HSSFWorkbook hssfWorkbook = new HSSFWorkbook();
        HSSFSheet hssfSheet = hssfWorkbook.createSheet();
        int rowNum = 0;
        //create header
        Collections.sort(excelVoList, new Comparator<ExcelVo>() {
            @Override
            public int compare(ExcelVo o1, ExcelVo o2) {
                return o1.getHeaderKey().compareTo(o2.getHeaderKey());
            }
        });
        HSSFFont headerFont = hssfWorkbook.createFont();
        headerFont.setColor(Font.COLOR_NORMAL);
//        headerFont.setBold(true);
        headerFont.setBoldweight(HSSFFont.BOLDWEIGHT_BOLD);
        HSSFCellStyle hssfCellStyle = hssfWorkbook.createCellStyle();
        hssfCellStyle.setFont(headerFont);

        HSSFRow hssfRow = hssfSheet.createRow(rowNum);
        for (int i = 0; i < excelVoList.size(); i++) {
            HSSFCell hssfCell = hssfRow.createCell(i);
            hssfCell.setCellValue(excelVoList.get(i).getHeaderValue());
            hssfCell.setCellStyle(hssfCellStyle);
        }
        for (Map<String, Object> info : data) {
            rowNum++;
            hssfRow = hssfSheet.createRow(rowNum);
            for (int i = 0; i < excelVoList.size(); i++) {
                HSSFCell hssfCell = hssfRow.createCell(i);
                hssfCell.setCellValue(Objects.toString(info.get(excelVoList.get(i).getHeaderKey()), ""));
            }
        }
        return hssfWorkbook;
    }

    public static HSSFWorkbook simpleExportMap(List<ExcelVo> excelVoList, List<Map> data) {
        HSSFWorkbook hssfWorkbook = new HSSFWorkbook();
        HSSFSheet hssfSheet = hssfWorkbook.createSheet();
        int rowNum = 0;
        createSheet(excelVoList, hssfWorkbook, hssfSheet);
        HSSFRow hssfRow = null;
        for (Map info : data) {
            rowNum++;
            hssfRow = hssfSheet.createRow(rowNum);
            //核心
            //根据行和列确定单元格坐标，并填充数据
            for (int i = 0; i < excelVoList.size(); i++) {
                HSSFCell hssfCell = hssfRow.createCell(i);
                hssfCell.setCellValue(Objects.toString(info.get(excelVoList.get(i).getHeaderKey()), ""));
            }
        }
        return hssfWorkbook;
    }

    static void createSheet(List<ExcelVo> excelVoList, HSSFWorkbook hssfWorkbook, HSSFSheet hssfSheet) {
        //HSSFSheet hssfSheet=hssfWorkbook.createSheet();
        int rowNum = 0;
        //create header
        Collections.sort(excelVoList, new Comparator<ExcelVo>() {
            @Override
            public int compare(ExcelVo o1, ExcelVo o2) {
                return o1.getHeaderKey().compareTo(o2.getHeaderKey());
            }
        });
        HSSFFont headerFont = hssfWorkbook.createFont();
        headerFont.setColor(Font.COLOR_NORMAL);
//        headerFont.setBold(true);
        headerFont.setBoldweight(HSSFFont.BOLDWEIGHT_BOLD);
        HSSFCellStyle hssfCellStyle = hssfWorkbook.createCellStyle();
        hssfCellStyle.setFont(headerFont);
        HSSFRow hssfRow = hssfSheet.createRow(rowNum);
        for (int i = 0; i < excelVoList.size(); i++) {
            HSSFCell hssfCell = hssfRow.createCell(i);
            hssfCell.setCellValue(excelVoList.get(i).getHeaderValue());
            hssfCell.setCellStyle(hssfCellStyle);
        }
    }


}
